package com.sinosoft.ebusiness.service.spring;


import com.alibaba.druid.util.StringUtils;
import com.sinosoft.ebusiness.service.face.ProducerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.task.TaskExecutor;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;


@Service("producerService")
@Lazy(true)
public class ProducerServiceImpl implements ProducerService{
	
	@Resource
	TaskExecutor proDuctTaskExecutor ;
	@Resource
    JmsTemplate jmsTemplate ;
	
	private  final Logger logger = LoggerFactory.getLogger(ProducerServiceImpl.class);
	

	/**
	 *指定 队列desination 发送 content内容
	 * 
	 * */
	public void sendMessage(final String desination,final String content){
		
		proDuctTaskExecutor.execute(new Runnable() {
			@Override
			public void run() {
				if(!StringUtils.isEmpty(content)){
						try {
					        jmsTemplate.send(desination, new MessageCreator() {
					            public Message createMessage(Session session) throws JMSException {   
					                return session.createTextMessage(content);   
					            }   
					        });
						} catch (Exception e) {
							logger.warn("JMS send Error,the errorInfo:{}",e.getMessage());
						}
				}
			}
		});
	}

}
